package com.mobilesales.android.activities.tasks;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

import com.mobilesales.android.R;
import com.mobilesales.android.adapters.CalendarAdapter;
import com.mobilesales.android.model.tasks.CyclicTask;
import com.mobilesales.android.model.tasks.Task;
import com.mobilesales.android.util.DateUtil;
import com.mobilesales.android.util.MessageBox;
import com.mobilesales.android.util.Settings;
import com.mobilesales.android.util.Temp;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.GridView;
import android.widget.TextView;

public class Tasks extends Activity {
	
	private GridView calGridView;
	private Calendar calendar;
	private TextView currentMonthLbl; 
	
	private Task[] tasks;
	private CyclicTask[] cyclicTasks;
	private int employeeId;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tasks);
        calGridView = (GridView)this.findViewById(R.id.calendar);
        currentMonthLbl = (TextView)this.findViewById(R.id.currentMonth);
        calendar = Calendar.getInstance();
        employeeId = Settings.getSettings().getEmployeeId();
        tasks = this.getTasks(employeeId);
        cyclicTasks = this.getCyclicTasks(employeeId);
        drawCalendar(calendar);
    }
    
    public void prevMonth(View v) {
    	calendar.add(Calendar.MONTH, -1);
    	this.drawCalendar(calendar);
    }
    
    public void nextMonth(View v) {
    	calendar.add(Calendar.MONTH, 1);
    	this.drawCalendar(calendar);
    }
    
    private void drawCalendar(Calendar calendar) {
    	int year = calendar.get(Calendar.YEAR); 
    	int month = calendar.get(Calendar.MONTH);
    	
    	int[] monthNameResIds = new int[] { R.string.January, R.string.February, R.string.March,
							    			R.string.April, R.string.May, R.string.June,
							    			R.string.July, R.string.August, R.string.September,
							    			R.string.October, R.string.November, R.string.December };
    	
    	currentMonthLbl.setText(String.format("%s %d", 
    			this.getString(monthNameResIds[month]), year));
    	List<Integer> occupiedDays = new ArrayList<Integer>();
    	if (cyclicTasks!=null && tasks!=null) {
    		occupiedDays = this.tasksToOccupiedDays(year, month, cyclicTasks, tasks);
    	}
    	else {
    		MessageBox.show(this, getString(R.string.ErrorTaskList));
    	}
    	CalendarAdapter adapter = CalendarAdapter.generateAdapter(this, year, month+1, occupiedDays);
    	calGridView.setAdapter(adapter);
    }
    
    
    private CyclicTask[] getCyclicTasks(int employeeId) {
    	// TODO: get cyclic tasks from local database
    	return Temp.getCyclicTasks();
    }

    private Task[] getTasks(int employeeId) {
    	// TODO: get tasks from local database
    	return Temp.getTasks();
    }
    
    
    private List<Integer> tasksToOccupiedDays(int year, int month, CyclicTask[] cyclicTasks, Task[] tasks) {
    	List<Integer> occupied = new ArrayList<Integer>();
    	GregorianCalendar calendar = new GregorianCalendar(year,month,1);
    	int dayInMonths = calendar.getActualMaximum(GregorianCalendar.DAY_OF_MONTH);
    	
    	if (cyclicTasks!=null) {
    		for(CyclicTask tsk : cyclicTasks) {
    			Date date = DateUtil.parseDate(tsk.StartTime);
    			calendar.setTime(date);

    	    	GregorianCalendar current = new GregorianCalendar(year,month,1);
    			for(int i=1; i<=dayInMonths; i++) {
    				current.set(year, month, i);
    				current.set(GregorianCalendar.HOUR_OF_DAY, calendar.get(GregorianCalendar.HOUR_OF_DAY));
    				current.set(GregorianCalendar.MINUTE, calendar.get(GregorianCalendar.MINUTE));
    				
    				long diff = Math.abs(current.getTimeInMillis() - calendar.getTimeInMillis())
    						 			/ (24 * 60 * 60 * 1000);
    				
    				if (diff % tsk.TaskInterval == 0) {
        				if (!occupied.contains(i))
            				occupied.add(i);
    				}
    			}

    		}
    	}
    	
    	if (tasks!=null) {
    		for(Task tsk : tasks) {
    			Date date = DateUtil.parseDate(tsk.StartTime);
    			if (date.getYear() == year && date.getMonth()==month) {
    				int day = date.getDate();
    				if (!occupied.contains(day))
        				occupied.add(day);
    			}
    		}
    	}  	    	
    	return occupied;
    }
}
